﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MobileSales.Model.Documents;
using MobileSales.Website.DocumentService;
using MobileSales.Website.Infrastructure;
using MobileSales.Common.Logger;
using MobileSales.Website.Infrastructure.Messaging;
using MobileSales.Model.Exceptions;
using System.ServiceModel;
using MobileSales.Website.OrderService;
using MobileSales.Model.Orders;
using MobileSales.Website.Models;
using MobileSales.Website.Localization;

namespace MobileSales.Website.Controllers
{
    [Authorize]
    public class DocumentsController : ControllerBase
    {
        #region Index
        //
        // GET: /Documents/

        public ActionResult Index()
        {
            IEnumerable<Document> documents = null;
            try
            {
                DocumentServiceClient documentService = new DocumentServiceClient();
                documentService.AddValidationToken(this.Token);
                documents = documentService.GetDocumentList();
                documentService.Close();
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorDocumentList, MessageType.Error);
            }
            return View(documents);
        } 
        #endregion

        #region Details
        //
        // GET: /Documents/Details/5

        public ActionResult Details(int id)
        {
            try
            {
                DocumentServiceClient documentService = new DocumentServiceClient();
                documentService.AddValidationToken(this.Token);
                Document doc = documentService.GetDocument(id);
                documentService.Close();
                return View(doc);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(InfoStrings.DocumentNotFound, MessageType.Error);
                return RedirectToAction("Index");
            }
        } 
        #endregion

        #region Create
        //
        // GET: /Documents/Create

        public ActionResult Create(int orderId)
        {
            OrderServiceClient orderService = new OrderServiceClient();
            orderService.AddValidationToken(this.Token);
            Order order = orderService.GetOrder(orderId);
            orderService.Close();
            if (order == null)
            {
                this.SetMessage(ErrorStrings.ErrorOrder, MessageType.Error);
                return RedirectToAction("Index");
            }
            DocumentModel model = new DocumentModel { OrderId = orderId, PaymentDate = DateTime.Now };
            ViewData["order"] = order;
            return View(model);
        }

        //
        // POST: /Documents/Create

        [HttpPost]
        public ActionResult Create(DocumentModel model)
        {
            try
            {
                DocumentServiceClient documentService = new DocumentServiceClient();
                documentService.AddValidationToken(this.Token);
                Document doc = documentService.CreateDocumentFromOrder(model.OrderId);
                doc.PaymentMethod = new PaymentMethod { Id = model.PaymentMethodId };
                doc.DocumentType = new DocumentType { Id = model.DocumentTypeId };
                doc.PaymentDate = model.PaymentDate;
                doc.IsPaid = model.IsPaid;
                documentService.StoreDocument(doc);
                documentService.Close();
                this.SetMessage(InfoStrings.DocumentCreated, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorDocumentCreating, MessageType.Error);

            }
            return Create(model.OrderId);
        } 
        #endregion

        #region Delete
        //
        // GET: /Documents/Delete/5

        public ActionResult Delete(int id)
        {
            return Details(id);
        }

        //
        // POST: /Documents/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                DocumentServiceClient documentService = new DocumentServiceClient();
                documentService.AddValidationToken(this.Token);
                documentService.DeleteDocument(id);
                documentService.Close();
                this.SetMessage(InfoStrings.DocumentDeleted, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorDocumentDeleting, MessageType.Error);
            }
            return RedirectToAction("Index");

        } 
        #endregion

        #region Pay
        [HttpPost]
        public ActionResult Pay(int documentId)
        {
            try
            {
                DocumentServiceClient documentService = new DocumentServiceClient();
                documentService.AddValidationToken(this.Token);
                documentService.Pay(documentId);
                documentService.Close();
                this.SetMessage(InfoStrings.DocumentPaid, MessageType.Info);
                return RedirectToAction("Index");
            }
            catch (FaultException<ValidationException> ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ex.Detail.Message, MessageType.Error);
            }
            catch (Exception ex)
            {
                CLogger.LogDebug(ex);
                this.SetMessage(ErrorStrings.ErrorDocumentPayment, MessageType.Error);
            }
            return RedirectToAction("Index");
        } 
        #endregion


    }
}
